<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function timeoutPromise(delay) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(`延迟了${delay}秒钟`)
                }, delay * 1000)
            })
        }

        const p1 = timeoutPromise(1)
        const p2 = timeoutPromise(2)
        const p3 = timeoutPromise(3)
        const p4 = timeoutPromise(4)
        const p5 = new Promise((resolve, reject) => {
            setTimeout(() => {
                reject('Warning:这个是err')
            }, 800)
        })

        //Promise.all里面传的是一个数组（数组的每一项都是一个promise）,
        //只有每一个promise都成功了，才会走进.then,有一个失败了就会走进.catch
        // Promise.all([p1, p2, p3, p4, p5])
        //     .then(res => console.log(res))
        //     .catch(res => console.log(res))

        Promise.race([p1, p2, p3, p4, p5])
            .then(res => console.log(res))
            .catch(res => console.log(res))
    </script>
</body>

</html>